package com.alquiler.dao;

import java.sql.Time;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import com.alquiler.dto.ItemDto;
import com.alquiler.entities.Tarifa;
import com.alquiler.implementation.GenericDaoImpl;
import com.alquiler.types.CATEGORIA;

public class TarifaDao extends GenericDaoImpl<Tarifa, Integer, ItemDto>{

	public TarifaDao(EntityManager entityManager) {
		super(entityManager);		
	}
	
	
	public Tarifa getTarifaByCategoriaAndHora(Time hora, CATEGORIA categoria){
		
		 Time horafinal = new Time(hora.getTime() + (60 * 60 * 1000));
    	 
		
		try {			
			Query query  =  entityManager.createQuery("Select t FROM " + Tarifa.class.getCanonicalName()+" t "
					+ "where t.categoria= :categoria "
					+ "and t.hora >= :hora and t.hora < :horafinal");
			 
			query.setParameter("categoria",categoria);		
			query.setParameter("hora",hora, TemporalType.TIME);	
			query.setParameter("horafinal",horafinal, TemporalType.TIME);	
			Tarifa resultList = (Tarifa) query.getSingleResult();
			return resultList; 
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

}
